package com.spark.data

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.google.gson.Gson
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.report
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import com.spark.common.net.toJsonBody
import com.spark.data.adapter.PersonalFolderAdapter
import com.spark.data.model.PersonalFolderListModel
import kotlinx.android.synthetic.main.frag_personal_folder.*

/**
 * ehhhhhh, personal folder.......
 */
class PersonalFolderFragment : BaseFragment() {
    private lateinit var personalFolderAdapter: PersonalFolderAdapter
    private var folder: PersonalFolderListModel? = null
    private var filesFilterFragment: FilesFilterFragment? = null

    companion object {
        fun newInstance(): PersonalFolderFragment {
            val fragment = PersonalFolderFragment()
            val args = Bundle()

            fragment.arguments = args
            return fragment
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_personal_folder
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { pop() }
        iv_category.setOnClickListener {
            filesFilterFragment = FilesFilterFragment.newInstance()
            filesFilterFragment?.show(childFragmentManager, "filefilter")
            filesFilterFragment?.addSearchActionListener {
                fetchPersonalFiles(keywords = it.keyword, privateFolder = it.directory)
            }
        }

        Const.getUser()?.apply {

        }
    }

    override fun onLazyInitView(savedInstanceState: Bundle?) {
        super.onLazyInitView(savedInstanceState)
        fetchPersonalFiles()
        folder = PersonalFolderListModel(resultMaps = mutableListOf(
                PersonalFolderListModel.ResultMap(name = "检修", fileList = mutableListOf(
                        PersonalFolderListModel.ResultMap.File(filename = "风机"),
                        PersonalFolderListModel.ResultMap.File(filename = "排气管"),
                        PersonalFolderListModel.ResultMap.File(filename = "过滤网")
                )),
                PersonalFolderListModel.ResultMap(name = "检修", fileList = mutableListOf(
                        PersonalFolderListModel.ResultMap.File(filename = "风机"),
                        PersonalFolderListModel.ResultMap.File(filename = "排气管"),
                        PersonalFolderListModel.ResultMap.File(filename = "过滤网")
                )),
                PersonalFolderListModel.ResultMap(name = "检修", fileList = mutableListOf(
                        PersonalFolderListModel.ResultMap.File(filename = "风机"),
                        PersonalFolderListModel.ResultMap.File(filename = "排气管"),
                        PersonalFolderListModel.ResultMap.File(filename = "过滤网")
                )),
                PersonalFolderListModel.ResultMap(name = "检修", fileList = mutableListOf(
                        PersonalFolderListModel.ResultMap.File(filename = "风机"),
                        PersonalFolderListModel.ResultMap.File(filename = "排气管"),
                        PersonalFolderListModel.ResultMap.File(filename = "过滤网")
                )),
                PersonalFolderListModel.ResultMap(name = "检修", fileList = mutableListOf(
                        PersonalFolderListModel.ResultMap.File(filename = "风机"),
                        PersonalFolderListModel.ResultMap.File(filename = "排气管"),
                        PersonalFolderListModel.ResultMap.File(filename = "过滤网")
                )),
                PersonalFolderListModel.ResultMap(name = "检修", fileList = mutableListOf(
                        PersonalFolderListModel.ResultMap.File(filename = "风机"),
                        PersonalFolderListModel.ResultMap.File(filename = "排气管"),
                        PersonalFolderListModel.ResultMap.File(filename = "过滤网")
                ))

        ))
        initAdapter()
    }

    override fun onBackPressedSupport(): Boolean {
        if (filesFilterFragment?.isVisible == true) {
            filesFilterFragment?.dismiss()
            return true
        }
        return super.onBackPressedSupport()
    }

    private fun initAdapter() {
        personalFolderAdapter = PersonalFolderAdapter(folder)
        exlv_files.setAdapter(personalFolderAdapter)
        exlv_files.setGroupIndicator(null)
        exlv_files.divider = null
        exlv_files.setOnGroupClickListener { parent, v, groupPosition, id -> return@setOnGroupClickListener false }
        exlv_files.setOnChildClickListener { parent, v, groupPosition, childPosition, id -> return@setOnChildClickListener false }
        folder?.resultMaps?.forEachIndexed { index, resultMap -> exlv_files.expandGroup(index) }
    }

    @SuppressLint("CheckResult")
    private fun fetchPersonalFiles(keywords: String = "", privateFolder: String = "") {
        val params = mutableMapOf(
                Pair(Const.qr_keyswords, keywords),
                Pair(Const.qr_privatefolderId, privateFolder)
        )
        Http.request()
                .rxPost("${Const.base_url}${Const.act_personal_files}", if (keywords.isNullOrBlank() && privateFolder.isNullOrBlank()) "{}".toJsonBody() else params.toJsonBody())
                .wrapIO()
                .subscribe(
                        {
                            Gson().fromJson(it, PersonalFolderListModel::class.java)?.apply {
                                if (code == "200") {
                                    this@PersonalFolderFragment.folder = this
                                    initAdapter()
                                } else {
                                    error(msg)
                                }
                            }
                        },
                        {
                            it.printStackTrace()
                            report(it)
                            hideLoader()
                        },
                        {
                            hideLoader()
                        },
                        {
                            dispose = it
                            loading()
                        }
                )
    }
}
